﻿#region Using's

using System;
using System.Collections.Generic;
using System.Linq;
using LightPersist.Transactions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MyUtils;
using TestDomainModel;
using System.Diagnostics;

#endregion

namespace LightPersistTests
{
	public abstract class ContextTestsBase : TestsBase
	{
		protected Guid childID;
		protected FakeControl child;
		protected TransactionGuard tr;

		protected void ChangeContext()
		{
			CommitIfNeed();
			tr.Dispose();
			RecreateContext();
			tr = Context.BeginTransaction();
		}

		protected void ChangeTransaction()
		{
			CommitIfNeed();
			tr.Dispose();
			tr = Context.BeginTransaction();
		}

		protected void ChangeTransactionWithUndoRedo()
		{
			CommitIfNeed();
			tr.Dispose();
			Trace.WriteLine("Test Undo...");
			Context.Undo();
			Trace.WriteLine("Test Redo...");
			Context.Redo();
			tr = Context.BeginTransaction();
		}

		/// <summary>
		/// Commit all transactions
		/// </summary>
		protected void CommitIfNeed()
		{
			int i = 0;
			while(Context.Reflect("currentTransaction").ToString() != "TR_NULL")
			{
				if(++i==20)
				{
					throw new Exception("Test: CommitIfNeed: too many nestd transactions: is it correct?");
				}
				Context.Reflect("CommitTransaction", Context.Reflect("currentTransaction"));
			}
		}

		[TestInitialize]
		public void ContextTestsBase_Init()
		{
			tr = Context.BeginTransaction();
			child = Context.Create<FakeControl>();
			Root.Children.Add(child);
			child.Name = "FIRST CHILD";
			child.Width = 55;
			childID = child.ID;
			ChangeTransaction();
			LogInitializeComplete();
		}

		protected virtual void Reconnect()
		{

		}

	}

	public class TripleTestsBase : ContextTestsBase
	{

	}

}